package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomDisViewPager;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.contract.adapter.MyPagerAdapter;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/23.
 *         <p/>
 *         ViewPager基类
 */
public  class MyBaseViewPagerFragment extends TheParentFragment implements ElasticScrollView.OnPullListener {
	public CustomDisViewPager mCustomPager ;
	public CirclePageIndicator mPagerInternal ;
	public ElasticScrollView mCustomScroll;
	public Button mConfirmBtn;
	public SlideBarView mSlideBarView;
	public StateButton mStateButton;
	public ArrayList<Fragment> fragmentList;
	public MyPagerAdapter mPagerAdapter;
	public LinearLayout mEmptyLayout;
	public TextView mEmptyTxt;
	@Override
	protected int getContentId() {
		return R.layout.base_viewpager_fragment;
	}

	@Override
	protected void init(View view) {
		mEmptyLayout = findView(view, R.id.ll_empty);
		mEmptyTxt = findView(view, R.id.tv_empty);
		mCustomPager = (CustomDisViewPager) view.findViewById(R.id.vp_content);
		mPagerInternal = (CirclePageIndicator) view.findViewById(R.id.pager_indicator);
		mCustomScroll = (ElasticScrollView) view.findViewById(R.id.ecl_custom);
		mConfirmBtn = (Button) view.findViewById(R.id.confirm_btn);
		mSlideBarView = findView(view, R.id.sbv_slide);
		mStateButton= (StateButton) view.findViewById(R.id.btn_state);
		mCustomScroll.setOnPullListener(this);
	}
	/**
	 * 显示搜索无结果界面
	 */
	public void ShowEmptyView() {
		mCustomPager.setVisibility(View.GONE);
		mEmptyLayout.setVisibility(View.VISIBLE);
		mPagerInternal.setVisibility(View.GONE);
		mEmptyTxt.setText(R.string.invite_search_no_result);
	}

	public void ShowEmptyView(CharSequence content) {
		mCustomPager.setVisibility(View.GONE);
		mEmptyLayout.setVisibility(View.VISIBLE);
		mPagerInternal.setVisibility(View.GONE);
		mEmptyTxt.setText(TextUtils.isEmpty(content)?getResources().getString(R.string.invite_search_no_result):content);
	}

	/**
	 * 显示无商品界面
	 */
	public void ShowEmptyGoodsView() {
		mCustomPager.setVisibility(View.GONE);
		mPagerInternal.setVisibility(View.GONE);
		mEmptyLayout.setVisibility(View.VISIBLE);
		mEmptyTxt.setText("没有商品信息");
	}
	public void showViewPagerView(){
		mCustomPager.setVisibility(View.VISIBLE);
		mPagerInternal.setVisibility(View.VISIBLE);
		mEmptyLayout.setVisibility(View.GONE);
	}
	/**
	 * 显示无商品界面
	 */
	public void ShowEmptyAddedGoodsView() {
		mCustomPager.setVisibility(View.GONE);
		mPagerInternal.setVisibility(View.GONE);
		mEmptyLayout.setVisibility(View.VISIBLE);
		mEmptyTxt.setText("没有商品数据");
	}

	/**
	 * 取消按钮的点击事件
	 */
	@Override
	public void cancelClick() {
		mCustomPager.setVisibility(View.VISIBLE);
		mEmptyLayout.setVisibility(View.GONE);
	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {

	}

	/**
	 *  @param total   总个数
	 * @param mNum     每一页的个数
	 * @param mBundle  Bundle 自定义数据
	 */
	public void SetFragmentAndPage(int total, int mNum, String fname, Bundle mBundle) {
		mEmptyLayout.setVisibility(View.GONE);
		mCustomPager.setVisibility(View.VISIBLE);
		int count = 1;
		fragmentList = new ArrayList<>();
		if (total > mNum) {
			if ((total - mNum) % mNum == 0) {
				count = (total - mNum) / mNum + 1;
			} else {
				count = (total - mNum) / mNum + 2; // 总共显示多少张卡片
			}
		} else {
			count = 1;
		}
		Bundle bundle = null;
		for (int i = 1; i <= count; i++) {
			if (mBundle != null) {
				bundle = new Bundle();
				bundle.putInt("pos", i);         //当前第几页,从1开始
				bundle.putInt("count", count);   //总页数
				bundle.putInt("num", mNum);      //每一页个数
				bundle.putAll(mBundle);
			}
			Fragment fragment = Fragment.instantiate(mActivity,fname);
			fragment.setArguments(bundle);
			fragmentList.add(fragment);
		}
		mPagerAdapter = new MyPagerAdapter(getChildFragmentManager(), fragmentList);
		mCustomPager.setAdapter(mPagerAdapter);
		if (count <= 6) {
			if (count > 1) {
				mPagerInternal.setVisibility(View.VISIBLE);
				mSlideBarView.setVisibility(View.GONE);
				mPagerInternal.setViewPager(mCustomPager, 0);
			} else {
				mPagerInternal.setVisibility(View.GONE);
				mSlideBarView.setVisibility(View.GONE);
			}
		} else {
			mPagerInternal.setVisibility(View.GONE);
			mSlideBarView.setVisibility(View.VISIBLE);
			mSlideBarView.setTotalPage(count);
			mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
				@Override
				public void onSlideChange(int page) {
					mCustomPager.setCurrentItem(page, false);
				}
			});
			mSlideBarView.setViewPagerInternal(mCustomPager);
		}
	}
	@Override
	public void onReLoad(int code) {

	}

	@Override
	public void onDownPull() {

	}

	@Override
	public void setTitle() {

	}

	@Override
	public void onUpPull() {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (EventBus.getDefault().isRegistered(this)){
			EventBus.getDefault().unregister(this);
		}
		((BaseCardDeskActivity) mActivity).hideSearchInput();
		((BaseCardDeskActivity)mActivity).hideSearchTitle();
	}
}